package com.jianwei.configuration;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Module("segment.enable")
@Configuration
public class SegmentDataSource {

    @Autowired
    private SegmentConfiguration configuration;

    @Bean
    public DynamicDataSource dynamicDataSource() {
        List<HikariDataSource> hikariDataSourceList = new ArrayList<>(configuration.getDbList().size());
        for (String db : configuration.getDbList()) {
            HikariConfig config = new HikariConfig("/" + db + ".properties");
            hikariDataSourceList.add(new HikariDataSource(config));
        }
        return new DynamicDataSource(hikariDataSourceList);
    }

}
